package com.salim.leetcode.$28;

public class StrStr {
    public int strStr(String haystack, String needle) {
        if(needle==null || needle.isEmpty()){
            return 0;
        }
        int haystackIndex = 0;
        while (haystackIndex<haystack.length()){
            if(haystack.charAt(haystackIndex) == needle.charAt(0)){
                if(haystack.length()-haystackIndex-1 >= needle.length()-1){
                    if(haystack.substring(haystackIndex,haystackIndex+needle.length()).equals(needle)){
                        return haystackIndex;
                    }else{
                        haystackIndex++;
                    }
                }else{
                    return -1;
                }
            }else{
                haystackIndex++;
            }
        }
        return -1;
    }


}
